home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 November / Macworld (1999-11).dmg / Updaters / WhiteCap 3.0.4 / WhiteCap Source.sit / WhiteCap Source / Common / io / Prefs.cpp < prev    next >
C/C++ Source or Header  |  1999-07-27  |  2KB  |  121 lines

  1. #include "Prefs.h"
  2.  
  3. #include "CEgIOFile.h"
  4. #include "EgOSUtils.h"
  5.  
  6. #ifdef EG_MAC
  7. #include <Files.h>
  8. #include <Folders.h>
  9. #endif
  10.  
  11.  
  12. Prefs::Prefs( const char* inPrefsName, bool inSysStored ) {
  13.     mSysStored    = inSysStored;
  14.     mDirty        = true;
  15.     
  16.     mPrefName.Assign( inPrefsName );
  17.     
  18.     #ifdef EG_MAC
  19.       short int    theVRef;
  20.     long        theDirID;
  21.     short        theErr;
  22.     FSSpec        prefSpec;
  23.     
  24.     if ( inSysStored ) {
  25.         theErr = ::FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &theVRef, &theDirID );            
  26.         if ( theErr != noErr ) {
  27.             theVRef = 0;
  28.             theDirID = 0;
  29.         } }
  30.     else {
  31.         theVRef        = ( (FSSpec*) EgOSUtils::sAppSpec.OSSpec() ) -> vRefNum;
  32.         theDirID    = ( (FSSpec*) EgOSUtils::sAppSpec.OSSpec() ) -> parID;
  33.     }    
  34.     ::FSMakeFSSpec( theVRef, theDirID, mPrefName.getPasStr(), &prefSpec );
  35.     mFileSpec.Assign( &prefSpec, 'TEXT' );
  36.     #endif
  37.     
  38.     #ifdef EG_WIN
  39.     // Note: mSysStored == true is unimplmented--just continue as mSysStored == false
  40.     // (yah, right--like i'm gonna even *think* about touching the registry!)
  41.     UtilStr prefPath;
  42.     prefPath.Assign( (char*) EgOSUtils::sAppSpec.OSSpec() );
  43.     prefPath.Append( mPrefName );
  44.     mFileSpec.Assign( prefPath.getCStr(), 0 );
  45.     #endif
  46. }
  47.  
  48.  
  49.  
  50. CEgErr Prefs::Load() {
  51.     CEgIFile iFile;
  52.     
  53.     mPrefs.Clear();
  54.     iFile.open( &mFileSpec );
  55.     mPrefs.SetArgs( &iFile );
  56.     
  57.     
  58.     if ( iFile.noErr() )
  59.         mDirty = false;
  60.     
  61.     return iFile;
  62. }
  63.  
  64.  
  65. CEgErr Prefs::Store() {
  66.     CEgIOFile oFile;
  67.     
  68.     if ( mDirty ) {
  69.         long origType = CEgIOFile::sCreatorType;
  70.         #if EG_MAC
  71.         CEgIOFile::sCreatorType = 'ttxt';
  72.         #elif EG_WIN
  73.         CEgIOFile::sCreatorType = '????';
  74.         #endif
  75.  
  76.         oFile.open( &mFileSpec );
  77.         
  78.         if ( oFile.noErr() ) {
  79.  
  80.             mPrefs.ExportTo( &oFile, true );
  81.             oFile.Writeln();
  82.         }
  83.         mDirty = false;
  84.         CEgIOFile::sCreatorType = origType;
  85.     }
  86.     
  87.     return oFile;
  88. }
  89.  
  90.  
  91. void Prefs::SetPref( long inID, const UtilStr& inData ) { 
  92.     
  93.     if ( ! mDirty ) {
  94.         const UtilStr* str;
  95.         
  96.         str = mPrefs.GetStr( inID );
  97.         if ( str ) {
  98.             if ( str -> compareTo( &inData ) )
  99.                 mDirty = true; }
  100.         else
  101.             mDirty = true;
  102.     }
  103.     
  104.     mPrefs.SetArg( inID, inData );
  105. }
  106.  
  107.  
  108. void Prefs::SetPref( long inID, long inData ) {
  109.     bool exists;
  110.     long num;
  111.     
  112.     if ( ! mDirty ) {
  113.     
  114.         exists = mPrefs.GetArg( inID, num );
  115.         if ( ! exists || num != inData )
  116.             mDirty = true;
  117.     }
  118.     
  119.     mPrefs.SetArg( inID, inData );
  120. }
  121.